package com.yjd.job.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.ResultModel;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.vo.Constant.ResultEnum;
import com.yjd.job.dao.IJobDao;
import com.yjd.job.model.JobModel;

/**
 * <b>description</b>：定时器服务类 <br>
 * <b>time</b>：2014-11-10下午1:38:17 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Service("jobService")
public class JobServiceImpl extends BaseServiceImpl implements IJobService {

	/**
	 * 新增，type不能重复
	 */
	@Override
	public ResultModel insert(JobModel model) throws Exception {
		ResultModel result = ResultModel.createResultModel();
		if (getJobModelByType(model.getType()) != null) {
			result.setResult(ResultEnum.ERROR);
			result.setMsgByResource("bizEx.job.typeisExists");
		} else {
			result.putValueToData("model", this.jobDao.insertModel(model, true));
		}
		return result;
	}

	/**
	 * 根据jobid删除job
	 */
	@Override
	public boolean delete(Long idJob) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id_job", idJob);
		this.deleteByMap(paramMap);
		return true;
	}

	/**
	 * 根据map删除对于的job
	 */
	@Override
	public boolean deleteByMap(Map<Object, Object> paramMap) throws Exception {
		this.jobDao.deleteModel(paramMap, true);
		return true;
	}

	/**
	 * 更新job，type不能重复
	 */
	@Override
	public ResultModel update(JobModel model) throws Exception {
		ResultModel result = ResultModel.createResultModel();
		JobModel tm = this.getJobModelByType(model.getType());
		if (tm != null && !tm.getType().equals(model.getType())) {
			result.setResult(ResultEnum.ERROR);
			result.setMsgByResource("bizEx.job.typeisExists");
		} else {
			this.jobDao.updateModel(model, true);
		}
		return result;
	}

	/**
	 * 根据job的类型获取job
	 */
	@Override
	public JobModel getJobModelByType(String type) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("type", type);
		return this.getModelOne(paramMap,DbWREnums.READ);
	}
	/**
	 * 查询入库的JOB
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<JobModel> queryDefinedJobs() throws Exception {
		return jobDao.getModelList(null, false);
	}
	/**
	 * job数据访问对象
	 */
	@Resource
	private IJobDao jobDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.jobDao;
	}

}
